<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Rbac;

use App\Controller\Admin\AdminController;
use App\Model\RbacRoleHasPermission;
use App\Repositories\AdminRepository;
use App\Repositories\RbacRoleRepository;
use App\Request\Rbac\StoreRoleRequest;
use App\Request\StoreAdminRequest;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class RoleController extends AdminController{
    public function all(){
        $search['id']=[
            'field' => 'id',
            'value' => '1',
            'method' => '!='
        ];
        $datas = AdminRepository::getInstance()->getList('*',$search,false);
        return $this->response->success($datas);
    }

    public function list(){
        $rows = $this->request->input('rows',10);
        $admin = $this->request->getAttribute('admin');
        $search = [];
        if($admin->role_id){
            $search['parent_id']=[
                'field' => 'parent_id',
                'value' => $admin->role_id
            ];
        }
        $datas = RbacRoleRepository::getInstance()->getList('*',$search,['limit'=>$rows]);
        return $this->response->success($datas);
    }

    public function parent(){
        $search['parent_id']=[
            'field' => 'parent_id',
            'value' => 0
        ];
        $datas = RbacRoleRepository::getInstance()->getList('*',$search,false);
        return $this->response->success($datas);
    }

    public function store(StoreRoleRequest $request){
        $params = $request->all();
        $result = RbacRoleRepository::getInstance()->store($params);
        if(!$result['success']){
            return $this->response->error(10055,$result['msg']);
        }
        return $this->response->success([
            'id' => $result['data']->id
        ]);
    }

    public function detail(){

    }

    public function delete(){

    }

    public function changeField()
    {
        $field = $this->request->input('field');
        $value = $this->request->input('value',false);
        $id = $this->request->input('id');
        $update =[
            $field=>$value=='true'?1:0
        ];
        $where=[
            [
                'field'=>'id',
                'value'=>$id
            ]
        ];
        $return = AdminRepository::getInstance()->update($update,$where);
        return $this->response->success([]);
    }

    public function permissions(){
        $role_id = $this->request->input('role_id',0);
        $columns=[
            'permission_id',
            'show_name',
            'name'
        ];
        $result = RbacRoleHasPermission::query()->select($columns)->where('role_id',$role_id)->join('rbac_permission',function($join){
            $join->on('rbac_role_has_permission.permission_id','=','rbac_permission.id');
        })->get();
        return $this->response->success($result);
    }

    public function configPermissionStore(){
        $role_id = $this->request->input('role_id',0);
        $permission_ids = $this->request->input('permission_ids','');
        $permission_ids = explode(',',$permission_ids);
        DB::beginTransaction();
        try{
            $select_ids = RbacRoleHasPermission::where('role_id',$role_id)
                ->get()->pluck('permission_id')->toArray();
            $delete = array_merge(array_diff($select_ids,$permission_ids));
            $add = array_merge(array_diff($permission_ids,$select_ids));
            if(!empty($delete)){
                RbacRoleHasPermission::where('role_id',$role_id)
                    ->whereIn('permission_id',$delete)->delete();
            }
            if(!empty($add)){
                foreach ($add as $value){
                    $model = new RbacRoleHasPermission();
                    $model->role_id = $role_id;
                    $model->permission_id = $value;
                    if(!$model->save()){
                        throw new \Exception('权限资源保存失败，请稍后重试。');
                    }
                }
            }
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(100415,$e->getMessage());
        }
        return $this->response->success([
        ]);


    }
}